package util;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    //定义成员变量
    private static DataSource ds;

    static {
        try {
            //1.加载配置文件
            Properties pro = new Properties();
            //可以使用classloader加载配置文件，获取字节输入流
            InputStream str = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(str);

            //2.初始化连接池对象
                ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * 获取连接池对象
     *
     * */
    public static DataSource getDataSource(){
        return ds;
    }

    /*
     * 获取连接Connection对象
     *
     * */

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
